<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div style="margin-top: 20px;">

    <div class="search_block">
        搜索：
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_code" name="s_code"  placeholder="订单号" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_name" name="s_name"  placeholder="按客户名" autocomplete="off">
            </div>
        </div>
        按日期:
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_predate" name="s_predate"  placeholder="从.." autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input class="layui-input" id="s_enddate" name="s_enddate"  placeholder="到.." autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn" id="search_btn" data-type="search">搜索</button>
                <label class="tip">${stateMsg}</label>
            </div>
        </div>
        <button class="layui-btn" style="float: right;padding-right: 10px" id="add_btn" data-type="add">添加新订单</button>
    </div>

    <table class="layui-hide" id="orders_table" lay-filter="orders_table"></table>

    <%--操作条--%>
    <script type="text/html" id="tool_bar">
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="items">订单详情管理</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑信息</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script>
        var util;
        layui.use(['form', 'laydate','table','layer','util'],function () {
            var form = layui.form,
                laydate=layui.laydate,
                table = layui.table,
                layer=layui.layer,
                $=layui.jquery;
                util=layui.util;
            laydate.render({
                elem:'#s_predate'
            });
            laydate.render({
                elem:'#s_enddate'
            });
            laydate.render({
                elem:'#indate'
            });
            form.verify({
                workerId:function (value) {
                    if(value.length<6){
                        return '工号不符合规则';
                    }
                },
                name:function (value) {
                    if(value==""){
                        return '姓名不能为空';
                    }
                }
            });
            form.on('submit(check)', function(data){
                return true;
            });

            table.render({
                elem: '#orders_table'
                ,even:true
                ,url:'/back/order/load'
                ,cols: [[
                    {field:'code', width:120, title: '订单号' ,sort: true}
                    ,{field:'customer',width:120,title:'客户',sort:true,templet: '#customerTpl'}
                    ,{field:'indate',width:120, title: '下单日期',templet: '#timeTpl',sort:true}
                    ,{field:'remark', title: '备注'}
                    ,{field:'isfinish',width:80,title:'完成？',sort:true,templet: '#finish'}
                    ,{field:'isout',width:80,title:'出货？',sort:true,templet: '#out'}
                    ,{field:'ispay',width:80,title:'收款？',sort:true,templet: '#pay'}
                    ,{fixed: 'right',title:'操作', width:250, align:'center', toolbar: '#tool_bar'}
                ]]
                ,done:function () {
                }
                ,id:'id'
                ,page: true
                ,limit:10
            });


            //操作按钮监听模块
            table.on('tool(orders_table)',function (obj) {
                var data=obj.data;
                var event=obj.event;

                if(event=='items'){
                    layer.open({
                        type: 2,
                        title:'个人信息',
                        skin: 'layui-layer-lan',
                        area:['1200px','680px'],
                        content: ["${pageContext.request.contextPath}/back/orderitem/byorder/list/"+data.id+"/"+data.code,'no'],
                        btn:'关闭页面',
                        yes:function (index) {
                            // layer.close(index);
                            location.reload();
                        }
                    });
                }

                if(event=='del'){
                    layer.confirm('要删除这个订单(请注意这个操作会删除对应的订单详情项)?', function(index){
                        $.post("${pageContext.request.contextPath}/back/order/delete",{id:data.id},function (state) {
                            var result=eval('('+state+')');
                            if(result){
                                layer.msg("删除成功");
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg("删除失败");
                                layer.close(index);
                            }
                        });

                    });
                }
                if(event=='edit'){
                    layer.open({
                        title:'修改页面'
                        ,type: 1
                        ,skin: 'layui-layer-lan'
                        ,area: ['420px','600px']
                        , content: $('#order_form')
                        ,success: function(layero, index){
                            $('#order_form').attr('action','${pageContext.request.contextPath}/back/order/modifyInfo');
                            $('#id').val(data.id);
                            $('#code').val(data.code);
                            $('#customer').find("option[value ='"+data.customer.id+"']").prop("selected","selected");
                            $('#remark').val(data.remark);
                            $('#indate').val(timeFormat(data.indate));
                            $('#btn').text("提交修改");
                            form.render();
                        }
                    });
                }
            });

            //搜索添加模块
            var $ = layui.$, active = {
                search: function(){
                    var s_code=$('#s_code').val();
                    var s_name = $('#s_name').val();
                    var s_predate=$('#s_predate').val();
                    var s_enddate=$('#s_enddate').val();
                    table.reload('id', {
                        where: {
                            s_code:s_code,
                            s_name:s_name,
                            s_predate:s_predate,
                            s_enddate:s_enddate
                        }
                        ,page: {
                            curr:1
                        }

                    });
                }
                ,add:function () {
                    layer.open({
                        title:'新订单信息录入'
                        ,type: 1
                        ,skin: 'layui-layer-lan'
                        ,area: ['420px','600px']
                        , content: $('#order_form')
                        ,success: function(layero, index){
                            $('#order_form').attr('action','${pageContext.request.contextPath}/back/order/add');
                            $('#id').val("");
                            $('#code').val("");
                            $('#indate').val("");
                            $('#remark').val();
                            $('#btn').text("提交添加");
                            form.render();
                        }
                    })
                }
            };

            $('#search_btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            $('#add_btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });

        //表格日期的格式化
        function timeFormat(t) {
            var date=new Date(t);
            return util.toDateString(date,'yyyy-MM-dd');
        }

        function customerFormat(data) {
            return data.name;
        }

        function boolformat(data) {
            if(data==true){
                return '是';
            }else{
                return '否';
            }
        }

    </script>
    <script type="text/html" id="timeTpl">
        {{timeFormat(d.indate)}}
    </script>
    <script type="text/html" id="customerTpl">
        {{customerFormat(d.customer)}}
    </script>

    <script type="text/html" id="finish">
        {{boolformat(d.isfinish)}}
    </script>
    <script type="text/html" id="out">
        {{boolformat(d.isout)}}
    </script>

    <script type="text/html" id="pay">
        {{boolformat(d.ispay)}}
    </script>
    <script type="text/html" id="">

    </script>
    <script type="text/html" id="">

    </script>

</div>

    <form id="order_form" style="display: none" class="layui-form" action="#" method="post">
        <input type="hidden" id="id" name="id"/>
        <div class="info_form_body">
            <div class="layui-form-item">
                <label class="layui-form-label">订单号:</label>
                <div class="layui-input-block">
                    <input id="code"  type="text" name="code"   lay-verify="required" placeholder="订单号" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">客户:</label>
                <div class="layui-input-block">
                    <select id="customer" name="customer.id">
                        <c:forEach var="customer" items="${customers}">
                            <option value="${customer.id}">${customer.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">下单日期:</label>
                <div class="layui-input-block">
                    <input id="indate"  type="text"  name="indate"  lay-verify="date"  autocomplete="off" class="layui-input"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注:</label>
                <div class="layui-input-block">
                    <textarea id="remark" name="remark" placeholder="备注" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button id="btn" lay-submit="" lay-filter="check" class="layui-btn"></button>
                </div>
            </div>
        </div>
    </form>